$(function(){
    var take = $.cookie("token");
    $.get("http://47.104.244.134:8080/cartlist.do",{token:take},function(data){
        //console.log(data);
        var str = "";
        for(var i = 0; i < data.length; i++){
            str += `<li data-id="${data[i].id}" data-gid="${data[i].gid}">
            <input type="checkbox"> 
            <span>${data[i].goods.name}</span>
            <img src ='${data[i].goods.picurl}'/>
            <span class="price">${data[i].goods.price}</span>
            <span class="minusBtn">-</span>
            <span class="count">${data[i].count}</span>
            <span class="plusBtn">+</span>
            <span class="perTotle">${data[i].count*data[i].goods.price}</span>
            <span class="delBtn">删除</span>
            </li>`
            // str+="<li data-id="+data[i].id+" data-gid="+data[i].gid+"><input type='checkbox'/><span>"+data[i].goods.name+"</span><img src='"+data[i].picurl+"'<span>"+data[i].goods.price+"</span><div id='div'><span class='minusBtn'>-</span><span class='count'>"+data[i].count+"</span><span class='plusBtn'>+</span></div><span class='perTotle'>"+data[i].count*data[i].goods.price+"</span><span class='delBtn'>删除</span></li>"
        }
        
        $(".content").html(str);
        
        //默认让购物车内所有的商品选中
        $(".content input").prop("checked",true);
        
        //计算所有商品的总价
        getTotlePrice();
        
        //点击加减号 更新购物车 更新价格
        
        $(".minusBtn").on("click",function(){
            changePrice(this,-1);
        })
        $(".plusBtn").on("click",function(){
            changePrice(this,1);
            
        })
        //点击复选框 更新总价
        $("input").on("click",function(){
            getTotlePrice();
        })
    })
    
    
    function getTotlePrice(){
        var sum = 0;
        $("input:checked").siblings(".perTotle").each(function(){
            sum += parseInt($(this).html());
        });
        $("#totalPrice").html(sum);
    }
    
    function changePrice(_this,num1){
        //点击加号
            if(num1==1){
                var num = $(_this).prev().html();
                $(_this).prev().html(++num);
            }
            //点击减号
            if(num1==-1){
                var num = $(_this).next().html();
                $(_this).next().html(--num);
            }
            // if()
            //更新每一行的总价
            var price = $(_this).parent().find(".price").html()*num;
            $(_this).parent().find(".perTotle").html(price)
            //计算所有商品的总价
            getTotlePrice();
            
            //更新购物车
            $.get("http://47.104.244.134:8080/cartupdate.do",{
                id:$(_this).parent().attr("data-id"),
                gid:$(_this).parent().attr("data-gid"),
                token:$.cookie("token"),
                num:num1
            })
    }
    
     //删除购物车
     $(document).on("click",".delBtn",function(){
                 var id=$(this).parent().attr("data-id");
                 var gid=$(this).parent().attr("data-gid");
               var take = $.cookie("token");
               $.get("http://47.104.244.134:8080/cartupdate.do",{"token":take,"id":id,"gid":gid,"num":0 }, 
           function (data){
               //打印id
            // console.log(goodsId) 
        }) ;
        //重新加载页面
        window.location.href = "car.html";
      }); 
       
      }); 

